<template>
  <div class="login">
    <div class="login-content">
      <div class="site-name padding-lr-sm">
        <div>
          <div class="name">{{ title }}{{ version }}</div>
          <div class="sub-name">{{ englishName.toUpperCase() }}</div>
        </div>
      </div>
      <div class="login-container">
        <div class="welcome padding-lr-sm blue">Hi，欢迎登录!</div>
        <van-form @submit="login" label-width="0">
          <van-field
            size="large"
            label=""
            left-icon="contact"
            v-model="form.account"
            placeholder="请输入工号"
            :rules="[{ required: true, message: '' }]"
          >
            <template #left-icon>
              <van-image :src="require('@/assets/login/user.png')" class="left-icon" />
            </template>
          </van-field>
          <van-field
            size="large"
            label=""
            type="password"
            v-model="form.password"
            placeholder="请输入密码"
            right-icon="eye-o"
            :rules="[{ required: true, message: '' }]"
            v-if="!isShowPassword"
            @click-right-icon="isShowPassword = true"
          >
            <template #left-icon>
              <van-image :src="require('@/assets/login/lock.png')" class="left-icon" />
            </template>
          </van-field>
          <van-field
            size="large"
            label=""
            right-icon="closed-eye"
            v-model="form.password"
            placeholder="请输入密码"
            :rules="[{ required: true, message: '' }]"
            v-if="isShowPassword"
            @click-right-icon="isShowPassword = false"
          >
            <template #left-icon>
              <van-image :src="require('@/assets/login/lock.png')" class="left-icon" />
            </template>
          </van-field>
          <div class="padding-lr-sm padding-tb-sm btn">
            <div class="red error" v-if="error">
              <van-icon name="warning" />
              {{ error }}
            </div>
            <van-button native-type="submit" type="info" block round>登录</van-button>
          </div>
        </van-form>
      </div>
    </div>

    <div class="footer" v-show="bottomIsShow">
      <p>{{ technicalSupport }}</p>
      <p>{{ copyright }} {{ version }}</p>
    </div>
  </div>
</template>

<script>
import { login } from '@/api/user'
import { title, englishName, version, technicalSupport, copyright } from '@/config'
export default {
  name: 'login',
  data() {
    return {
      form: {
        type: 'job_no'
      },
      isShowPassword: false,
      title,
      englishName,
      technicalSupport,
      copyright,
      version,
      error: '',
      bottomIsShow: true, //控制底部tabbar显示与隐藏
      defaultPhoneHeight: 0, //屏幕默认高度
      nowPhoneHeight: 0 //屏幕现在的高度
    }
  },
  created() {
    this.docmHeight = document.documentElement.clientHeight
  },
  mounted() {
    this.defaultPhoneHeight = window.innerHeight
    window.onresize = () => {
      //这个方法会被调用两次,软键盘弹出后和软件收起后
      this.nowPhoneHeight = window.innerHeight
    }
  },
  watch: {
    nowPhoneHeight: function () {
      if (this.defaultPhoneHeight != this.nowPhoneHeight) {
        // 当软键盘弹出,改变bottomIsSHow变量然后去控制bottom的显示与隐藏
        this.bottomIsShow = false
      } else {
        // 相等代表软键盘收起后,与上同理
        this.bottomIsShow = true
      }
    }
  },
  destroyed() {
    //页面销毁前要记得这个监听消除掉;
    window.onresize = null
  },
  methods: {
    login() {
      login(this.form)
        .then(res => {
          localStorage.setItem('token', res.token)
          localStorage.setItem('userInfo', JSON.stringify(res.user))
          this.$router.replace('/')
        })
        .catch(err => {
          // 失败
          localStorage.setItem('token', 12345)
          this.$router.replace('/')
          console.log(err)
          this.error = err.msg
        })
    }
  }
}
</script>

<style scoped lang="less">
@import 'index';
</style>
